package com.ycode.bang.anbang.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ycode.bang.anbang.MyApp;
import com.ycode.bang.anbang.R;
import com.ycode.bang.anbang.baseui.BaseActivity;
import com.ycode.bang.anbang.model.OrderDetil;
import com.ycode.bang.anbang.network.FinalHttpUtil;
import com.ycode.bang.anbang.utils.RequestMapDev;
import com.ycode.bang.anbang.utils.ToastUtil;

import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONObject;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 申请转派
 */
public class Change extends BaseActivity implements View.OnClickListener {
    @InjectView(R.id.edit_all)
    EditText mEdit;
    @InjectView(R.id.btn_againdeal)
    Button mBtnAd;
    @InjectView(R.id.title)
    TextView title;

    private String INTERFACE_PATH_TRAN = "";//接口路径//申请转派
    private int tag;//
    private OrderDetil orderDetil = new OrderDetil();
    private int id;//
    private int orderId;//

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change);
        ButterKnife.inject(this);
        if (this.getIntent().getExtras() != null) {
            Bundle bundle = this.getIntent().getExtras();
            tag = bundle.getInt("tag");
            id = bundle.getInt("id");
            orderId = bundle.getInt("orderId");
        }
        initView();
        getPath();
    }

    public void initView() {
        title.setText("申请转派");
        mBtnAd.setOnClickListener(this);
    }

    public void getPath() {
        if (tag == 1) {
            INTERFACE_PATH_TRAN = RequestMapDev.TransferOrder;
        } else if (tag == 2) {
            INTERFACE_PATH_TRAN = RequestMapDev.TransferOrder2;
        } else if (tag == 3) {
            INTERFACE_PATH_TRAN = RequestMapDev.TransferOrder3;
        }
    }


    @Override
    protected void getSuccessRequest(Object response, int flag) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_againdeal:
                tranOrder();
                break;
        }
    }

    /**
     * 申请转派
     */
    private void tranOrder() {
        AjaxParams mAjaxParam = new AjaxParams();
        mAjaxParam.put("csrfToken", MyApp.instance.getToken());
        if (tag == 2) {
            mAjaxParam.put("assignmentRecordId", id + "");
        } else {
            mAjaxParam.put("id", orderId + "");
        }
        mAjaxParam.put("transferReason", mEdit.getText().toString().trim());
        FinalHttpUtil.post(INTERFACE_PATH_TRAN, mAjaxParam, new AjaxCallBack<String>() {
            @Override
            public void onSuccess(String o) {
                super.onSuccess(o);
                //放置数据
                try {
                    JSONObject jsonObject = new JSONObject(o.toString());
                    String status = jsonObject.getString("status");
                    String msg = jsonObject.getString("message");
                    String data = jsonObject.getString("data");
                    if (status.equals("0")) {
                        Log.e("这个接口是成功的", o);
                        ToastUtil.show(Change.this, "申请转派成功");
                        finish();
                    } else {
                        ToastUtil.show(Change.this, msg.toString());
                    }
                } catch (Exception e) {
                    ToastUtil.show(Change.this, e.toString());
                }
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                loginout_550(errorNo);
//                ToastUtil.show(Change.this, strMsg);
            }
        });
    }
}
